package com.crm.service.impl.customerManager;


import com.crm.dao.mapper.customerManager.ChsCustomerTagMapper;
import com.crm.model.entity.customerManager.ChsCustomerTag;
import com.crm.model.entity.customerManager.ChsCustomerTagExample;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.customerManager.CustomerTagDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @ Author ：baijian
 * @ Date   ：2021/5/7 11:10
 */
@Service
public class CustomerTagDbServiceImpl implements CustomerTagDbService {

    @Autowired
    private ChsCustomerTagMapper chsCustomerTagMapper;


    @Override
    public List<ChsCustomerTag> selectCustomerList() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        ChsCustomerTagExample chsCustomerTagExample =new ChsCustomerTagExample();
        ChsCustomerTagExample.Criteria criteria = chsCustomerTagExample.createCriteria();
        criteria.andCreatePersonIdEqualTo(userAccount.getAccountId());
        chsCustomerTagExample.setOrderByClause("create_tm asc");
        return chsCustomerTagMapper.selectByExample(chsCustomerTagExample);
    }

    @Override
    public Boolean addTag(ChsCustomerTag chsCustomerTag) {
        Boolean result = false;
        int num = chsCustomerTagMapper.insertSelective(chsCustomerTag);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public Boolean deleteTag(String id) {
        Boolean result = false;
        int num = chsCustomerTagMapper.deleteByPrimaryKey(id);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public int selectTagNameSize(String tag) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        ChsCustomerTagExample chsCustomerTagExample =new ChsCustomerTagExample();
        ChsCustomerTagExample.Criteria criteria = chsCustomerTagExample.createCriteria();
        criteria.andCreatePersonIdEqualTo(userAccount.getAccountId());
        criteria.andTagEqualTo(tag);
        return chsCustomerTagMapper.selectByExample(chsCustomerTagExample).size();
    }

    @Override
    public int selectTagSize(String tag) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        ChsCustomerTagExample chsCustomerTagExample =new ChsCustomerTagExample();
        ChsCustomerTagExample.Criteria criteria = chsCustomerTagExample.createCriteria();
        criteria.andCreatePersonIdEqualTo(userAccount.getAccountId());
        return chsCustomerTagMapper.selectByExample(chsCustomerTagExample).size();
    }
}
